// miniprogram/pages/my/my.js
const app = getApp();
const dd=wx.cloud.database()
const db=wx.cloud.database().collection('user')
const db2=wx.cloud.database().collection('community')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    ismyfavor:"active",
    isempty:false,
    isvideoempty:true,
    isother:'',
    myfavor:[],
    mycollect:[],
    mycollect_video:[],
    myfavor_video:[]
  },
  go:function(){
   wx.switchTab({
     url: '../community/community',
   })
  },
  menu_favor:function(){
    const that=this;
    that.setData({
      ismyfavor:'active',
      isother:''
    })
    console.log(that.data.myfavor)
   
  },
  menu_other:function(){
    const that=this;
    that.setData({
      ismyfavor:'',
      isother:'active',
    })



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  into:function(e){
    const that=this
    var index=e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../detail/detail?content='+JSON.stringify(that.data.mycollect[index]),
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that=this
   
    that.setData({
      isempty:true,
      isvideoempty:true
    })
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    app.getOpenId.then(res => {
      that.setData({
        openid:res
      })
      
      db.doc(res).get({
        success: function(res) {
          console.log(res.data)
          that.setData({
            myfavor:res.data.collect,
            myfavor_video:res.data.collect_video
          })
          console.log(res.data.collect.length)
          if(res.data.collect.length!=0){
            that.setData({
              isempty:false,
            })
          }
          if(res.data.collect_video.length!=0){
            that.setData({
              isvideoempty:false,
            })
          }

          const _=dd.command
          db2.where({
            _id: _.in(that.data.myfavor)
          }).get({
            success: function(res1) {
              // res.data 是包含以上定义的两条记录的数组
              console.log(res1.data)
              that.setData({
                mycollect:res1.data,
              })
             
            }
          })
          dd.collection('video').where({
            _id: _.in(that.data.myfavor_video)
          }).get({
            success: function(res1) {
              // res.data 是包含以上定义的两条记录的数组
              console.log(res1.data)
              that.setData({
                mycollect_video:res1.data,
              })
             
            }
          })
        },
        fail:function(){
          that.setData({
            isempty:true,
            isvideoempty:true
          })
        }
      })
    })
    wx.hideLoading({
      success: (res) => {},
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})